package com.scwatch.core.service.impl;

import com.scwatch.common.orm.Limitable;
import com.scwatch.common.orm.SearchFilter;
import com.scwatch.core.domain.PushMsg;
import com.scwatch.core.repository.PushMsgDao;
import com.scwatch.core.service.PushMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@Service
@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
public class PushMsgServiceImpl implements PushMsgService {
    @Override
    public Page<PushMsg> findAll(final  Integer siteId,Map<String, String[]> params, Pageable pageable) {
        //注意：user.id
        return pushMsgDao.findAll(spec(siteId, params), pageable);
    }

    @Override
    public List<PushMsg> findAll(final  Integer siteId,Map<String, String[]> params, Limitable limitable) {
        //注意：user.id
        return pushMsgDao.findAll(spec(siteId, params), limitable);
    }

    @Override
    @Transactional
    public PushMsg save(PushMsg bean) {
        bean = pushMsgDao.save(bean);
        em.flush();
        em.refresh(bean);
        return bean;
    }

    @Override
    public PushMsg findById(Integer id) {
        return pushMsgDao.findOne(id);
    }

    @Transactional
    public void delete(PushMsg bean) {
        pushMsgDao.delete(bean);
    }

    @Transactional
    public PushMsg[] delete(Integer[] ids) {
        PushMsg[] beans = new PushMsg[ids.length];
        for (int i = 0; i < ids.length; i++) {
            PushMsg invite = findById(ids[i]);
            delete(invite);
            beans[i] = invite;
        }
        return beans;
    }

    private Specification<PushMsg> spec(final Integer siteId, Map<String, String[]> params) {
        Collection<SearchFilter> filters = SearchFilter.parse(params).values();
        final Specification<PushMsg> fsp = SearchFilter.spec(filters, PushMsg.class);
        Specification<PushMsg> sp = new Specification<PushMsg>() {
            public Predicate toPredicate(Root<PushMsg> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate pred = fsp.toPredicate(root, query, cb);
                if (siteId != null) {
                    pred = cb.and(pred, cb.equal(root.get("site").get("id"), siteId));
                }
                return pred;
            }
        };
        return sp;
    }


    @Autowired
    private PushMsgDao pushMsgDao;

    @Autowired
    private EntityManager em;

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }
}
